//
//  MiMaViewController.m
//  BeautyPicture
//
//  Created by LiuNa on 16/3/9.
//  Copyright © 2016年 szb. All rights reserved.
//

#import "MiMaViewController.h"
#import "MBProgressHUD+MJ.h"
#import "BPhttpTool.h"
#import "AccountTool.h"
#import "BPLoginViewController.h"

@interface MiMaViewController ()<UITextFieldDelegate>{
    UITextField *_phoneNmuber;
    UITextField *_passWord;
    UITextField *_NpassWord;
    UIButton *_BPloginBtn;
}
@end

@implementation MiMaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self create];
    
    // Do any additional setup after loading the view.
}


- (void)create
{
    NSArray *placeArry = [NSArray arrayWithObjects:@"请输入您现在用的密码",@"请输入6~20位的新密码", @"请再输入一次新密码",nil];
    
    CGFloat textY = 64;
    CGFloat textH = 47.0f;
    CGFloat textX = 20.0f;
    CGFloat textW = BPScreenW - 40.0f;
    
    UILabel *tele = [[UILabel alloc] initWithFrame:CGRectMake(textX, 64, 70, textH)];
    tele.text = @"原密码:";
    tele.textAlignment = 1;
    tele.font = [UIFont systemFontOfSize:18];
//    tele.alpha = 0.5;
    [self.view addSubview:tele];
    
    _phoneNmuber = [[UITextField alloc] initWithFrame:CGRectMake(textX + 70, textY, textW, textH)];
    _phoneNmuber.delegate = self;
    _phoneNmuber.placeholder = placeArry[0];
//    NSString *phone = [[NSUserDefaults standardUserDefaults] objectForKey:@"passWord"];
//    if (phone) {
//        _phoneNmuber.text = phone;
//    }
    [self.view addSubview:_phoneNmuber];
    
    
    
    UILabel *mima = [[UILabel alloc] initWithFrame:CGRectMake(textX, _phoneNmuber.y + textH, 70, textH)];
    mima.text = @"新密码:";
    mima.textAlignment = 1;
    mima.font = [UIFont systemFontOfSize:18];
//    mima.alpha = 0.5;
    [self.view addSubview:mima];
    
    _passWord = [[UITextField alloc] init];
    _passWord.secureTextEntry = YES;
    //    _passWord.frame = _phoneNmuber.bounds;
    _passWord.frame = CGRectMake(textX + 70, textY, textW, textH);
    _passWord.y = _phoneNmuber.y + textH;
    _passWord.x = textX + 70;
    _passWord.delegate = self;
    _passWord.placeholder = placeArry[1];
    [self.view addSubview:_passWord];
    
    
    UILabel *mimas = [[UILabel alloc] initWithFrame:CGRectMake(textX-20, mima.y + textH, 90, textH)];
    mimas.text = @"再次确认:";
    mimas.textAlignment = 1;
//    mimas.alpha = 0.5;
    mimas.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:mimas];
    
    _NpassWord = [[UITextField alloc] initWithFrame:CGRectMake(textX + 70, mima.y + textH, textW, textH)];
    _NpassWord.frame = CGRectMake(textX + 70, textY, textW, textH);
    _NpassWord.y = mima.y + textH;
    _NpassWord.x = textX + 70;
    _NpassWord.placeholder = placeArry[2];
    _NpassWord.secureTextEntry = YES;
    [self.view addSubview:_NpassWord];
    
    
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(25, _NpassWord.y + _NpassWord.height + 40, self.view.frame.size.width - 50, 30)];
    button.backgroundColor = BPGlobaThemeColor;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 15;
    [button setTitle:@"确认修改" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(LoginIn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    
    for (int i = 0; i < 3; i ++) {
        BPLineView *line = [[BPLineView alloc] initWithFrame:CGRectMake(80, textY + textH * (i+1), BPScreenW, 0.7)];
        [self.view addSubview:line];
    }
}

- (void)LoginIn{
    if ([AccountTool account].uid != nil) {
        
//        if (![_phoneNmuber.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"passWord"]]){
//            [MBProgressHUD showError:@"原密码不正确"];
        //        }else
        if ([_phoneNmuber.text isEqualToString:@""] || [_passWord.text isEqualToString:@""] || [_NpassWord.text isEqualToString:@""]) {
            
            [MBProgressHUD showError:@"请把信息填写完整"];
            
        }else if (![_phoneNmuber.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"passWord"]]){
            [MBProgressHUD showError:@"原密码不正确"];
        }
        else if (![_passWord.text isEqualToString:_NpassWord.text]) {
            [MBProgressHUD showError:@"再次确认密码不正确"];
        }else if ([_passWord.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"passWord"]]){
            [MBProgressHUD showError:@"新密码与原密码相同，请重新设置"];
        }
        else{
            [MBProgressHUD showMessage:@"正在修改"];
            
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            [param setValue:_phoneNmuber.text forKey:@"old"];
            [param setValue:_passWord.text forKey:@"new"];
            [param setValue:[AccountTool account].uid forKey:@"uid"];
            
            [BPhttpTool post:[NSString stringWithFormat:@"%@user/profile",BPhttp] params:param success:^(id responseObj) {
                [MBProgressHUD hideHUD];
                [MBProgressHUD showError:@"修改成功"];
                BPLoginViewController *login = [[BPLoginViewController alloc] init];
                login.mimaLogin = YES;
                [self.navigationController pushViewController:login animated:YES];
                login.block = ^(){
                    
                };
                
            } failure:^(NSError *error) {
                
            }];
        }
    }else{
        [MBProgressHUD showError:@"请您先登录"];
    }
   
    
} 





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
